#include "BookFileDialog.h"

BookFileDialog::BookFileDialog(int op)
{
    this->op = op;

    layout_main = new QGridLayout;

    label_file = new QLabel();

    input_file = new QLineEdit();

    button_op = new QPushButton();
}

BookFileDialog::~BookFileDialog()
{
    delete button_op;

    delete input_file;

    delete label_file;

    delete layout_main;
}

void BookFileDialog::init_view_main()
{
    setFixedSize(360, 100);

    layout_main->setSpacing(10);    //设置控件距离
    setLayout(layout_main);

    label_file->setStyleSheet("QLabel{font:12px;padding: 5px 15px 5px 15px;}");

    label_file->setText("文件：");

    layout_main->addWidget(label_file, 0, 0, 1, 1);
    layout_main->addWidget(input_file, 0, 1, 1, 10);

    if(op == 0)
    {
        button_op->setText("导入");
        setWindowTitle("书单导入");
    }
    else
    {
        button_op->setText("导出");
        setWindowTitle("书单导出");
    }
    input_file->setPlaceholderText("请输入文件路径或目录路径");
    input_file->setToolTip("*.csv文件");
    input_file->setStyleSheet("QLabel{font:15px;padding: 0px 5px 0px 5px;}");
    input_file->setClearButtonEnabled(true);

    QObject::connect(button_op,&QPushButton::clicked,button_op,[&](){
        QString file_name = input_file->text().isEmpty() ? NULL : input_file->text();
        if(file_name.endsWith("/") || file_name.endsWith("\\")) // 默认状态下，文件名为："书单.csv"
        {
            file_name += "书单.csv";
        }
        if(op == 0)
        {
            setWindowTitle("导入中...");
            if(input(file_name))
            {
                setWindowTitle("导入成功");
            }
            else
            {
                setWindowTitle("导入失败");
            }
        }
        else
        {
            setWindowTitle("导出中...");
            if(output(file_name))
            {
                setWindowTitle("导出成功");
            }
            else
            {
                setWindowTitle("导出失败");
            }
        }

    }); //设置监听
    layout_main->addWidget(button_op, 0, 12, 1, 1);
}

int BookFileDialog::input(QString file_name)
{
    if(!file_name.endsWith(".csv"))
    {
        return 0;
    }

    QFile file(file_name);
    QString line;    //读入内容
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return 0;
    }
    QTextStream input_stream(&file);
    //    input_stream.setCodec("UTF-8");

    SQLHelper sql_helper;
    sql_helper.connect();
    QVector<QString> v_book;

    line = input_stream.readLine(); //读入标题
    while (!input_stream.atEnd()) {
        line = input_stream.readLine().toUtf8();
        std::cout << line.toStdString() << std::endl;
        v_book.clear();

        for(int i = 0; i < 3; i++)
        {
            QString val = line.section(',', i, i);
            if(val.isNull())
            {
                std::cout << "value is NULL!" << std::endl;
                return 0;
            }
            v_book.push_back(val);

        }
        sql_helper.insert_table("books", &v_book);
    }

    file.flush();
    file.close();

    return 1;
}

int BookFileDialog::output(QString file_name)
{
    if(!file_name.endsWith(".csv"))
    {
        return 0;
    }

    QFile file(file_name);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        return 0;
    }

    SQLHelper sql_helper;
    sql_helper.connect();

    QVector<QVector<QString>*> result;
    if(!sql_helper.search_table("books",NULL, NULL, &result))
    {
        std::cout << "selete books failed!" << std::endl;
        return 0;
    }

    file.write("编号,书名,作者\n");  //写入标题
    for(int i = 0; i < result.size(); i++)
    {
        file.write(((*result[i])[0] + "," + (*result[i])[1] + "," + (*result[i])[2] + "\n").toUtf8());
    }

    file.flush();
    file.close();
    return 1;
}
